package financier.jfz.com.babycare;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.jfz.quick.core.db.LightOrmHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import financier.jfz.com.babycare.base.BaseActivity;
import financier.jfz.com.babycare.bean.AddItemBean;
import financier.jfz.com.babycare.bean.BaseModle;

public class AddMethodActivity extends BaseActivity {
    RecyclerView recyclerView;
    List<AddItemBean> data = new ArrayList<>();
    MyAdapter adapter;
    BaseModle baseModle;
    public final static String TitleName = "titleName";
    public final static String MSG = "msg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_method);
        initUI();
    }

    private void initUI() {
        if (getIntent().getSerializableExtra(MSG) != null) {
            baseModle = (BaseModle) getIntent().getSerializableExtra(MSG);
        }

        getToolbar().setRightTitleText("保存");
        getToolbar().setRightTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (data.size() > 0) {

                    baseModle.setData(data);
                    LightOrmHelper.getInstent().save(baseModle);
                    Toast.makeText(AddMethodActivity.this, "模型创建成功！", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editDialog();
            }
        });
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyAdapter(data);
        recyclerView.setAdapter(adapter);
        if (baseModle == null) {
            baseModle = new BaseModle();
            baseModle.setId(UUID.randomUUID().toString());
            baseModle.setName(getIntent().getStringExtra(TitleName));
        } else {
            setMsg();
        }
        getToolbar().setMainTitle(baseModle.getName());
    }

    private void setMsg() {
        data.addAll(baseModle.getData());
        adapter.notifyDataSetChanged();

    }

    class MyAdapter extends RecyclerView.Adapter<AddHold> {
        private List<AddItemBean> data;

        MyAdapter(List<AddItemBean> data) {
            this.data = data;
        }

        public void add(AddItemBean addItemBean) {
            this.data.add(addItemBean);
        }

        @NonNull
        @Override
        public AddHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.add_item_layout, viewGroup, false);
            return new AddHold(view);
        }

        @Override
        public void onBindViewHolder(@NonNull AddHold viewHolder, int i) {
            viewHolder.tvName.setText(data.get(i).name);
            viewHolder.tvType.setText("类型：" + data.get(i).typeName);
        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }

    public class AddHold extends RecyclerView.ViewHolder {
        public TextView tvName, tvType;

        public AddHold(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvType = itemView.findViewById(R.id.tv_type);
            itemView.findViewById(R.id.item_msg_group).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editDialog(data.get(getAdapterPosition()), getAdapterPosition());
                }
            });
        }
    }


    EditText dialogEditText;

    private void editDialog() {
        editDialog(null, -1);
    }

    private void editDialog(final AddItemBean addItemBean, final int index) {
        Spinner dialogSpinner;
        View view = LayoutInflater.from(this).inflate(R.layout.editdialog, null);
        final String[] strings = getResources().getStringArray(R.array.study_view_spinner_values);
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (TextUtils.isEmpty(dialogEditText.getText())) {
                            Toast.makeText(AddMethodActivity.this, "标题不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        dialogInterface.dismiss();
                        AddItemBean resultAddItemBean = addItemBean;
                        if (resultAddItemBean == null) {
                            resultAddItemBean = new AddItemBean();
                            resultAddItemBean.parentId = baseModle.getId();
                            resultAddItemBean.id = UUID.randomUUID().toString();
                        }

                        resultAddItemBean.name = dialogEditText.getText().toString();
                        if (dialogEditText.getTag() == null || strings[0].equals(dialogEditText.getTag())) {
                            resultAddItemBean.typeName = strings[0];
                            resultAddItemBean.type = "num";

                        } else {
                            resultAddItemBean.typeName = strings[1];
                            resultAddItemBean.type = "string";
                        }
                        if (addItemBean == null) {
                            adapter.add(resultAddItemBean);
                            adapter.notifyItemInserted(data.size() - 1);
                        } else {
                            data.set(index, resultAddItemBean);
                            adapter.notifyItemChanged(index);
                        }

                    }
                });

        dialogEditText = view.findViewById(R.id.tv_name);
        dialogSpinner = view.findViewById(R.id.spinner_simple);
        if (addItemBean != null) {
            builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    data.remove(index);
                    adapter.notifyItemRemoved(index);
                }
            });
            dialogEditText.setText(addItemBean.name);
            if ("num".equals(addItemBean.type)) {
                dialogSpinner.setSelection(0, true);
            } else {
                dialogSpinner.setSelection(1, true);
            }
        }
        dialogSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                dialogEditText.setTag(strings[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        builder.create().show();

    }
}
